library(dplyr)
library(readstata13)


## set to Datavers folder

survey <- read.dta13("Harris_Data/Harris 1976 Blacks in America Survey, study no. 7683/harris_s7683_blacks_spss.dta")

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- as.character(7683)

# study year (year)
survey$year <- 1976

# geographic data (urban)
summary(survey$S13)
survey$urban <- as.character(survey$S13)

# geographic data (region)
survey$region <- as.character(survey$S11)
table(survey$region)

# respondent head of household (hh)
survey$hh <- as.character(survey$F1)
table(survey$hh)

# increasing inequality (inequality)
survey$inequality <- as.character(survey$Q7_2)
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
survey$union.self <- as.character(survey$F6_1)
survey$union.self[survey$F6_4 == "Yes"] <- "Not Sure"

survey$union.other <- as.character(survey$F6_2)
survey$union.other[survey$F6_4 == "Yes"] <- "Not Sure"

# employment (employed)
survey$employed <- as.character(survey$F2A)
table(survey$employed)

# self em
survey$employed.self <- NA

# occupation
survey$occupation <- as.character(survey$F2B)
table(survey$occupation)

# oc self
survey$occupation.self <- NA 

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
survey$educ <- as.character(survey$F5)
table(survey$educ)                        

# household income (income)
survey$income <- as.character(survey$F7)
table(survey$income)

# age
survey$age <- as.character(survey$F4)

# race
survey$race <- as.character(survey$F10)
table(survey$race) # all are black

# politics (party)
survey$party <- as.character(survey$P3D)

# politics (ideology)
survey$ideology <- as.character(survey$P3A)
table(survey$ideology)

# gender
survey$gender <- as.character(survey$F11)

# religion
survey$religion <- as.character(survey$F8) 

## factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- as.character(survey$Q7_1)
survey$dontcount <- as.character(survey$Q7_4)
survey$leftout <- as.character(survey$Q7_5)

## question place
## it's a bit odd b/c recipients of the "black"
## questionaaire were not supposed to be asked the 
## politics question, but some did - in the white
## questionnarie the inequality question was placed
## before the party question - will go with that

survey$question_place <- "before party"



# subset
survey_7683blacks <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                               "inequality", "inequality.variable", "union.self", "union.other",
                               "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                               "age", "race", "party", "ideology", "gender", "religion",
                               "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                               "question_place")]


####### NOW THE SAME THING FOR WHITE RESPONDENTS
#################################################################333

survey <- read.dta13("Harris_Data/Harris 1976 Blacks in America Survey, study no. 7683/harris_s7683_whites_spss.dta")

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- as.character(7683)

# study year (year)
survey$year <- 1976

# geographic data (urban)
summary(survey$S13)
survey$urban <- as.character(survey$S13)

# geographic data (region)
survey$region <- as.character(survey$S11)
table(survey$region)

# respondent head of household (hh)
survey$hh <- as.character(survey$F1)
table(survey$hh)

# increasing inequality (inequality)
survey$inequality <- as.character(survey$Q7_2)
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
survey$union.self <- as.character(survey$F6_1)
survey$union.self[survey$F6_4 == "Yes"] <- "Not Sure"

survey$union.other <- as.character(survey$F6_2)
survey$union.other[survey$F6_4 == "Yes"] <- "Not Sure"

# employment (employed)
survey$employed <- as.character(survey$F2A)
table(survey$employed)

# self em
survey$employed.self <- NA

# occupation
survey$occupation <- as.character(survey$F2B)
table(survey$occupation)

# oc self
survey$occupation.self <- NA 

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
survey$educ <- as.character(survey$F5)
table(survey$educ)                        

# household income (income)
survey$income <- as.character(survey$F7)
table(survey$income)

# age
survey$age <- as.character(survey$F4)

# race
survey$race <- as.character(survey$F10)
table(survey$race) # all are black

# politics (party)
survey$party <- as.character(survey$P3D)
table(survey$party)

# politics (ideology)
survey$ideology <- as.character(survey$P3A)
table(survey$ideology)

# gender
survey$gender <- as.character(survey$F11)

# religion
survey$religion <- as.character(survey$F8) 

## factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- as.character(survey$Q7_1)
survey$dontcount <- as.character(survey$Q7_4)
survey$leftout <- as.character(survey$Q7_5)

## quesiton_place
survey$question_place <- "before party"

# subset
survey_7683whites <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                               "inequality", "inequality.variable", "union.self", "union.other",
                               "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                               "age", "race", "party", "ideology", "gender", "religion",
                               "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                               "question_place")]

#######################################
# #  # # # # combine both files
#######################################

survey_7683 <- rbind(survey_7683blacks, survey_7683whites)



survey_7683$pid <- c(1:nrow(survey_7683))


#saveRDS(survey_7683, file = "Harris_Data/survey_7683.rds")
